package observer.improve;

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者
 * 相当于 ConcreteSubject
 */
public class WeatherDataSubject implements Subject {
    private final List<Observer> observers;   //所有观察者
    @Getter
    private float temperature;
    @Getter
    private float pressure;
    @Getter
    private float humidity;

    public WeatherDataSubject() {
        this.observers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    /**
     * 变更温度
     */
    public void updateTemperature(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        // 通知所有观察者
        notifyObserver();
    }

    /**
     * 通知所有的观察者
     */
    @Override
    public void notifyObserver() {
        for (Observer observer : observers) {
            observer.update(temperature, pressure, humidity);
        }
    }
}
